<template>
  <div class="login">
    <topBack>登录注册</topBack>
    <div class="loginForm">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" @click="onLogin">登录</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import topBack from "../components/topBack";
export default {
  components: {
    topBack
  },
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
    },
    onLogin() {
      this.axios({
        methods: "get",
        url: "http://leeyiqing.site/login.php",
        params: {
          username: this.username,
          password: this.password
        }
      }).then(res => {
        console.log(res);

        if (res.data == "error") {
          console.log("账户名或者密码错误!!!");
        } else {
          this.$router.push('user')
          this.$store.commit('onLogin',true)
        }
      }).catch(err => cconsole.log("账户名或者密码错误!!!"))
    }
  }
};
</script>

<style lang="less" scoped>
.login {
  .loginForm {
    padding-top: 44px;
  }
}
</style>